package org.freemarker.web.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.freemarker.core.dto.GenderStatistics;
import org.freemarker.core.model.Gender;
import org.freemarker.web.custom.CustomHttpServlet;

import com.google.gson.Gson;

public class TestController extends CustomHttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String dogName = getServletContext().getInitParameter("dogName");
		
		Gson jsonObject = new Gson();
		List<GenderStatistics> gsList = new ArrayList<GenderStatistics>(); 
		
		GenderStatistics gsm = contactService.getContactCountOnGender(Gender.MALE);
		gsList.add(gsm);
		
		GenderStatistics gsf = contactService.getContactCountOnGender(Gender.FEMALE);
		gsList.add(gsf);

		// convert java object to JSON format, and returned as JSON formatted string
		String json = jsonObject.toJson(gsList);
	 
		resp.setContentType("application/json");
		// Get the printwriter object from response to write the required json object to the output stream      
		PrintWriter out = resp.getWriter();
		out.println(json);
		out.flush();
	}

	/*
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		getServletConfig().getServletContext();
//		req.getReader()
		Gson jsonObject = new Gson();
		List<GenderStatistics> gsList = new ArrayList<GenderStatistics>(); 
		
		GenderStatistics gsm = contactService.getContactCountOnGender(Gender.MALE);
		gsList.add(gsm);
		
		GenderStatistics gsf = contactService.getContactCountOnGender(Gender.FEMALE);
		gsList.add(gsf);

		// convert java object to JSON format, and returned as JSON formatted string
		String json = jsonObject.toJson(gsList);
	 
		resp.setContentType("application/json");
		// Get the printwriter object from response to write the required json object to the output stream      
		PrintWriter out = resp.getWriter();
		out.println(json);
		out.flush();
//		out.close();
//		req.getRequestDispatcher("testCalendarV3.ftl").forward(req, resp);
	}*/
	
	/*	
  	private String parseString(String s){
		String	parsed[] = s.split(":");
		int size = parsed[1].length();
		if(parsed[1].startsWith("//")){
			return parsed[1].substring(2, size);
		}
			
		return parsed[1];
	}
	*/

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		super.doPost(req, resp);
	}
}
